﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TrainsAndTimeTables.ViewModels;
using RailroadDal;

namespace TrainsAndTimeTables.Views
{
    /// <summary>
    /// Interaction logic for TrainsAndTimeTablesView.xaml
    /// </summary>
    public partial class TrainsAndTimeTablesView : UserControl
    {
        public TrainsAndTimeTablesView()
        {
            InitializeComponent();
            var viewModel = (this.DataContext as TrainsViewModel);
          
            foreach (var item in viewModel.Stations)
            {
                
                Ellipse circle = new Ellipse();
                canvas.Children.Add(circle);
                circle.Width = 10;
                circle.Height = 10;
                circle.Name = "id";
                circle.Name += item.StationId.ToString();
                Canvas.SetTop(circle, item.OffsetY-5);
                Canvas.SetLeft(circle, item.OffsetX-5);
                //circle.Margin = new Thickness(item.OffsetX, item.OffsetY, 0, 0);
                circle.Opacity = 0.5;
                circle.Fill = Brushes.Blue;
                circle.MouseLeftButtonDown += this.Circle_leftBottonDown;
                
            }
        }

        public void Circle_leftBottonDown(object sender, MouseButtonEventArgs e)
        {
            Ellipse circle = sender as Ellipse;
            int stationId = int.Parse(circle.Name.Substring(2));
            var viewModel = (this.DataContext as TrainsViewModel);
            viewModel.UpdateRautes.Execute(stationId);
            this.rautes.ItemsSource = viewModel.RauteAsString;
            this.rautes.Visibility = System.Windows.Visibility.Visible;
        }

        private void rautes_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index=this.rautes.SelectedIndex;
            DrawTimeTable(index);
            DrawRautes(index);
        }

        private void DrawTimeTable(int rauteIndex)
        {
            var viewModel = (this.DataContext as TrainsViewModel);
            Raute raute = viewModel.StationRautes[rauteIndex];
            int id = raute.RouteId;
            viewModel.UpdateTable.Execute(id);
            dgTimeTable.ItemsSource = viewModel.TimeTable;
            dgTimeTable.Visibility = System.Windows.Visibility.Visible;
        }

        private void DrawRautes(int rauteIndex) 
        {
            var lines = this.canvas.Children.OfType<Line>().ToList();

            for (int i = 0; i < lines.Count; i++)
            {
                this.canvas.Children.Remove(lines[i]);
            }

            var labels = this.canvas.Children.OfType<Label>().ToList();

            for (int i = 0; i < labels.Count; i++)
            {
                this.canvas.Children.Remove(labels[i]);
            }
            var viewModel = (this.DataContext as TrainsViewModel);
            Raute raute = viewModel.StationRautes[rauteIndex];
            List<Station> stations = raute.Stations.ToList<Station>();
            for (int i = 0; i < stations.Count; i++)
            {
                if (i + 1 < stations.Count) 
                {
                    Label label = new Label();
                    label.Content = viewModel.TimeTable[i].Depart;
                    label.FontSize = 20;
                    Canvas.SetTop(label, stations[i].OffsetY-50);
                    Canvas.SetLeft(label, stations[i].OffsetX-20);
                    label.Foreground = Brushes.Green;
                    this.canvas.Children.Add(label);
                    Line line = new Line();
                    line.Stroke =Brushes.Blue;
                    line.StrokeThickness = 4;
                    line.X1 = stations[i].OffsetX;
                    line.Y1 = stations[i].OffsetY;
                    line.X2 = stations[i+1].OffsetX;
                    line.Y2 = stations[i+1].OffsetY;
                    this.canvas.Children.Add(line);
                }
            }
        }
    }
}
